﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Matthew.DBManager
{
    //public static class IDataBase
    //{
    //    bool AddUser(UserInfo userInfo);

    //    bool DeleteUser(string userName);

    //    bool ChangeUserInfo(UserInfo userInfo);
    //}


    public class DataBaseManager //: IDataBase
    {
        private static string connString = @"Data Source=10.2.94.22;Initial Catalog=UserInfoDatabase;User Id=sa;Password=1qaz2wsxE";
        //private static string connString = @"Data Source=MATTHEW-PC;Initial Catalog=UserInfoDatabase;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False";

        public static bool CreateAllTable()
        {
            StringBuilder sqlBuilder = new StringBuilder();
            #region
            sqlBuilder.Append(@"use UserInfoDatabase
  create table UserInfo  (
	UserName varchar(20),
    DisplayName varchar(20),
	UserPsw varchar(20),
    UserRole varchar(10),
	UserBalance int,
	)
     
     CREATE CLUSTERED INDEX UserName
     ON UserInfo(UserName)
          WITH FILLFACTOR= 30");

            sqlBuilder.Append(@" create table UserOrderInfo  (
	UserName varchar(20),
	UserOrder varchar(20),
	UserCost varchar(10),
	UserOrderTime varchar(20),
    FlagDay varchar(10),
	)
	
CREATE CLUSTERED INDEX UserName
     ON UserOrderInfo(UserName)
          WITH FILLFACTOR= 30");

            sqlBuilder.Append(@" create table UserExpected (
    UserName varchar(20),
	UserExpected varchar(50),
    FlagDay varchar(10),
	)
	
CREATE CLUSTERED INDEX UserName
     ON UserExpected(UserName)
          WITH FILLFACTOR= 30");

            sqlBuilder.Append(@" create table AdminManagerInfo  (
	UserName varchar(20),
    UserCanOrder bit,
    WhatToEat int,
	)
     
     CREATE CLUSTERED INDEX UserName
     ON AdminManagerInfo(UserName)
          WITH FILLFACTOR= 30");
            #endregion

            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = sqlBuilder.ToString();
                    cmd.ExecuteNonQuery();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        public static bool AddUserInfo(UserInfo userInfo)
        {
            return InsertIntoUserInfo(userInfo);
        }

        //测试int型cost数据
        public static bool AddUserOrderInfo(string userName, List<UserOrderInfo> userOrders)
        {
            return InsertIntoUserOrderInfo(userName, userOrders);
        }

        public static bool AddUserExpected(string userName, List<int> userExpecteds, string flagDay)
        {
            StringBuilder builder = new StringBuilder("values");
            //传入list
            foreach (var userExpected in userExpecteds)
            {
                builder.Append("('").Append(userName).Append("', ").Append(userExpected).Append(", '").Append(flagDay).Append("'),");
            }
            string strSql = builder.Remove(builder.Length - 1, 1).ToString();

            try
            {
                //lock (strSql)
                //{
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = @"Insert into
   [UserInfoDatabase].[dbo].[UserExpected]" + strSql;
                    cmd.ExecuteNonQuery();
                }
                //}
            }
            catch (Exception e)
            {
                return false;
            }

            return true;
        }

        //删除user以及所有订单
        public static bool DeleteUserInfo(string userName)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = @"delete [UserInfoDatabase].[dbo].[UserInfo] where UserName=@UserName
  delete [UserInfoDatabase].[dbo].[UserOrderInfo] where UserName=@UserName
    delete [UserInfoDatabase].[dbo].[UserExpected] where UserName=@UserName";
                    cmd.Parameters.AddWithValue("@UserName", userName);
                    cmd.ExecuteNonQuery();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        //测试int型cost数据
        //删除指定value,cost，flagday的订单
        public static bool DeleteUserOrderInfo(string userName, List<UserOrderInfo> orders)
        {
            StringBuilder builder = new StringBuilder();
            foreach (var orderInfo in orders)
            {
                builder.Append(" (UserOrder='").Append(orderInfo.UserOrder).Append("' and UserCost=").Append(orderInfo.UserCost).Append(" and UserOrderTime='").Append(orderInfo.UserOrderTime).Append("' and FlagDay='").Append(orderInfo.FlagDay).Append("') or");
            }
            string orderSql = builder.Remove(builder.Length - 2, 2).ToString();
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = @"delete [UserInfoDatabase].[dbo].[UserOrderInfo] where UserName=@UserName and" + orderSql;
                    cmd.Parameters.AddWithValue("@UserName", userName);
                    cmd.ExecuteNonQuery();
                }
            }
            catch
            {
                return false;
            }

            return true;
        }

        public static bool DeleteUserExpected(string userName, string flagDay)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = @"delete [UserInfoDatabase].[dbo].[UserExpected] where UserName=@UserName
and FlagDay=@FlagDay";
                    cmd.Parameters.AddWithValue("@UserName", userName);
                    cmd.Parameters.AddWithValue("@FlagDay", flagDay);
                    cmd.ExecuteNonQuery();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        public static bool ChangeUserInfo(UserInfo userInfo)
        {
            return UpdateUserInfo(userInfo);
        }

        public static bool ChangeUserBalance(string userName, int userBalance)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = @"update [UserInfoDatabase].[dbo].[UserInfo] 
set UserBalance=@UserBalance
 where UserName=@UserName";
                    cmd.Parameters.AddWithValue("@UserName", userName);
                    cmd.Parameters.AddWithValue("@UserBalance", userBalance);
                    cmd.ExecuteNonQuery();
                }

            }
            catch
            {
                return false;
            }
            return true;
        }

        //方法没用，delete and add
        static bool ChangeUserOrderInfo(string userName, List<UserOrderInfo> orders)
        {
            return UpdateUserOrderInfo(userName, orders);
        }

        public static UserInfo GetUserInfo(string userName, bool withOrderInfos)
        {
            if (withOrderInfos)
            {
                UserInfo info = ExecuteUserInfo(userName);
                info.UserOrderInfos = ExecuteUserOrderInfo(userName);
                return info;
            }
            return ExecuteUserInfo(userName);
        }

        public static List<UserInfo> GetUserInfoFromUserNameOrDisplayName(string userName)
        {
            List<UserInfo> result = new List<UserInfo>();
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "SELECT * from UserInfo where UserName=@UserName or DisplayName=@DisplayName";
                    cmd.Parameters.AddWithValue("@UserName", userName);
                    cmd.Parameters.AddWithValue("@DisplayName", userName);
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        UserInfo info = new UserInfo();
                        info.UserName = reader["UserName"].ToString();
                        info.DisplayName = reader["DisplayName"].ToString();
                        info.UserPsw = reader["UserPsw"].ToString();
                        info.UserRole = reader["UserRole"].ToString();
                        info.UserBalance = Convert.ToInt32(reader["UserBalance"]);
                        result.Add(info);
                    }
                }
            }
            catch { }
            return result;
        }

        public static List<UserOrderInfo> GetUserAllOrderInfos(string userName)
        {
            return ExecuteUserOrderInfo(userName);
        }

        public static UserExpected GetUserAllExpectedOrders(string userName)
        {
            return ExecuteUserExpected(userName, "");
        }

        public static List<UserOrderInfo> GetUserSpecialOrderInfos(string userName, string flagDay)
        {
            return ExecuteUserOrderInfo(userName, flagDay);
        }

        public static List<UserInfo> GetOrdersByFlagDay(string flagDay)
        {
            return ExecuteUserOrderInfoByFlagDay(flagDay);
        }

        //return list<string>就可以
        public static UserExpected GetUserSpecialExpectedOrder(string userName, string flagDay)
        {
            return ExecuteUserExpected(userName, flagDay);
        }

        public static void GetExpectedOrdersOfSpecialDay(string flagDay)
        {
            List<UserOrderInfo> orderInfos = new List<UserOrderInfo>();
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "SELECT max(UserExpected) from UserOrderInfo where FlagDay=@FlagDay";
                    cmd.Parameters.AddWithValue("@FlagDay", flagDay);
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        UserOrderInfo info = new UserOrderInfo();
                        info.UserOrder = reader["UserOrder"].ToString();
                        info.UserOrderTime = reader["UserOrderTime"].ToString();
                        info.UserCost = Convert.ToInt32(reader["UserCost"]);
                        info.FlagDay = flagDay;
                        orderInfos.Add(info);
                    }
                }
            }
            catch
            {
            }
        }

        public static List<MostExpectedInfo> GetMostExpectedOrders(string flagDay)
        {
            List<MostExpectedInfo> result = new List<MostExpectedInfo>();
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "select top 3 Count(userexpected) Count, UserExpected FROM UserExpected where FlagDay=@FlagDay group by UserExpected";
                    cmd.Parameters.AddWithValue("@FlagDay", flagDay);
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        MostExpectedInfo info = new MostExpectedInfo();
                        info.Shop = (Orders)Convert.ToInt32(reader["UserExpected"]);
                        info.Count = Convert.ToInt32(reader["Count"]);
                        result.Add(info);
                    }
                }
            }
            catch
            {
                //注意打log
            }
            return result;
        }

        public static List<UserInfo> GetUserInfo()
        {
            return ExecuteUserInfo();
        }

        public static bool ChangeUserPassWord(string userName, string passWord)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = @"update [UserInfoDatabase].[dbo].[UserInfo] 
set UserPsw=@UserPsw
 where UserName=@UserName";
                    cmd.Parameters.AddWithValue("@UserName", userName);
                    cmd.Parameters.AddWithValue("@UserPsw", passWord);
                    cmd.ExecuteNonQuery();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        public static bool InsertAdminManagerInfo(string userName, bool canOrder)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = @"Insert into
   [UserInfoDatabase].[dbo].[AdminManagerInfo]
   values(@UserName, @UserCanOrder, @WhatToEat);";
                    cmd.Parameters.AddWithValue("@UserName", userName);
                    cmd.Parameters.AddWithValue("@UserCanOrder", canOrder);
                    cmd.Parameters.AddWithValue("@WhatToEat", 0);
                    cmd.ExecuteNonQuery();
                }

            }
            catch
            {
                return false;
            }
            return true;
        }

        public static bool UpdateAdminManagerInfo(AdminInfo info)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "Update AdminManagerInfo set UserCanOrder=@UserCanOrder, WhatToEat=@WhatToEat";
                    cmd.Parameters.AddWithValue("@UserCanOrder", info.IfCanOrder);
                    cmd.Parameters.AddWithValue("@WhatToEat", Convert.ToInt32(info.WhatToEat));
                    cmd.ExecuteNonQuery();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static AdminInfo GetAdminManagerInfo()
        {
            AdminInfo result = new AdminInfo();
            result.Administrators = new List<string>();
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "select * from AdminManagerInfo";
                    SqlDataReader reader = cmd.ExecuteReader();
                    //reader.read读两遍好吗？？？？？？？？？？？？？？？？？？
                    //if (reader.Read())
                    //{
                    //    result.IfCanOrder = Convert.ToBoolean(reader["UserCanOrder"]);
                    //    result.WhatToEat = (Orders)Convert.ToInt32(reader["WhatToEat"]);
                    //}
                    if (reader.Read())
                    {
                        result.Administrators.Add(reader["UserName"].ToString());
                        result.IfCanOrder = Convert.ToBoolean(reader["UserCanOrder"]);
                        result.WhatToEat = (Orders)Convert.ToInt32(reader["WhatToEat"]);
                    }
                }
            }
            catch
            {
                //注意打log:
            }
            return result;
        }

        #region
        private static bool InsertIntoUserInfo(UserInfo userInfo)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = @"Insert into
   [UserInfoDatabase].[dbo].[UserInfo]
   values(@UserName, @DisplayName, @UserPsw, @UserRole, @UserBalance);";
                    cmd.Parameters.AddWithValue("@UserName", userInfo.UserName);
                    cmd.Parameters.AddWithValue("@DisplayName", userInfo.DisplayName);
                    cmd.Parameters.AddWithValue("@UserPsw", userInfo.UserPsw);
                    cmd.Parameters.AddWithValue("@UserRole", userInfo.UserRole);
                    cmd.Parameters.AddWithValue("@UserBalance", userInfo.UserBalance);
                    cmd.ExecuteNonQuery();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        private static bool InsertIntoUserOrderInfo(string userName, List<UserOrderInfo> orderInfos)
        {
            foreach (var orderInfo in orderInfos)
            {
                try
                {
                    using (SqlConnection conn = new SqlConnection(connString))
                    {
                        conn.Open();
                        SqlCommand cmd = conn.CreateCommand();
                        cmd.CommandText = @"Insert into
   [UserInfoDatabase].[dbo].[UserOrderInfo]
   values(@UserName, @UserOrder, @UserCost, @UserOrderTime, @FlagDay);";
                        cmd.Parameters.AddWithValue("@UserName", userName);
                        cmd.Parameters.AddWithValue("@UserOrder", orderInfo.UserOrder);
                        cmd.Parameters.AddWithValue("@UserCost", orderInfo.UserCost);
                        cmd.Parameters.AddWithValue("@UserOrderTime", orderInfo.UserOrderTime);
                        cmd.Parameters.AddWithValue("@FlagDay", orderInfo.FlagDay);
                        cmd.ExecuteNonQuery();
                    }
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }

        private static UserInfo ExecuteUserInfo(string userName)
        {
            UserInfo info = new UserInfo();
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "SELECT * from UserInfo where UserName=@UserName";
                    cmd.Parameters.AddWithValue("@UserName", userName);
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        info.UserName = reader["UserName"].ToString();
                        info.DisplayName = reader["DisplayName"].ToString();
                        info.UserPsw = reader["UserPsw"].ToString();
                        info.UserRole = reader["UserRole"].ToString();
                        info.UserBalance = Convert.ToInt32(reader["UserBalance"]);
                    }
                }
            }
            catch { }
            return info;
        }

        private static List<UserInfo> ExecuteUserInfo()
        {
            List<UserInfo> userInfos = new List<UserInfo>();
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT * from UserInfo";
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    UserInfo info = new UserInfo();
                    info.UserName = reader["UserName"].ToString();
                    info.DisplayName = reader["DisplayName"].ToString();
                    info.UserPsw = reader["UserPsw"].ToString();
                    info.UserRole = reader["UserRole"].ToString();
                    info.UserBalance = Convert.ToInt32(reader["UserBalance"]);
                    userInfos.Add(info);
                }
            }
            //ExecuteUserOrderInfo(userInfos);
            return userInfos;
        }

        private static List<UserOrderInfo> ExecuteUserOrderInfo(string userName)
        {
            List<UserOrderInfo> orderInfos = new List<UserOrderInfo>();
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT * from UserOrderInfo where UserName=@UserName";
                cmd.Parameters.AddWithValue("@UserName", userName);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    UserOrderInfo info = new UserOrderInfo();
                    info.UserOrder = reader["UserOrder"].ToString();
                    info.UserOrderTime = reader["UserOrderTime"].ToString();
                    info.UserCost = Convert.ToInt32(reader["UserCost"]);
                    info.FlagDay = reader["FlagDay"].ToString();
                    orderInfos.Add(info);
                }
            }
            return orderInfos;
        }

        private static List<UserOrderInfo> ExecuteUserOrderInfo(string userName, string flagDay)
        {
            List<UserOrderInfo> orderInfos = new List<UserOrderInfo>();
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT * from UserOrderInfo where UserName=@UserName and FlagDay=@FlagDay";
                cmd.Parameters.AddWithValue("@UserName", userName);
                cmd.Parameters.AddWithValue("@FlagDay", flagDay);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    UserOrderInfo info = new UserOrderInfo();
                    info.UserOrder = reader["UserOrder"].ToString();
                    info.UserOrderTime = reader["UserOrderTime"].ToString();
                    info.UserCost = Convert.ToInt32(reader["UserCost"]);
                    info.FlagDay = flagDay;
                    orderInfos.Add(info);
                }
            }
            return orderInfos;
        }

        private static List<UserInfo> ExecuteUserOrderInfoByFlagDay(string flagDay)
        {
            List<UserInfo> users = new List<UserInfo>();
            List<string> userNames = new List<string>();
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "SELECT * from UserOrderInfo where FlagDay=@FlagDay";
                    cmd.Parameters.AddWithValue("@FlagDay", flagDay);
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        UserInfo user;
                        UserOrderInfo info = new UserOrderInfo();
                        info.UserOrder = reader["UserOrder"].ToString();
                        info.UserOrderTime = reader["UserOrderTime"].ToString();
                        info.UserCost = Convert.ToInt32(reader["UserCost"]);
                        info.FlagDay = flagDay;
                        string userName = reader["UserName"].ToString();
                        if (!userNames.Contains(userName))
                        {
                            userNames.Add(userName);
                            user = new UserInfo();
                            user.UserName = userName;
                            user.UserOrderInfos = new List<UserOrderInfo>();
                            user.UserOrderInfos.Add(info);
                            users.Add(user);
                        }
                        else
                        {
                            user = users.Find(m => m.UserName == userName);
                            user.UserOrderInfos.Add(info);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                //注意打log

            }
            return users;
        }

        private static UserExpected ExecuteUserExpected(string userName, string flagDay)
        {
            UserExpected result = new UserExpected();
            result.FlagDay = flagDay;
            result.UserName = userName;
            result.UserExpecteds = new List<int>();
            try
            {
                string strSql = "SELECT * from [UserInfoDatabase].[dbo].[UserExpected] where UserName=@UserName and FlagDay=@FlagDay";
                if (string.IsNullOrEmpty(flagDay))
                {
                    strSql = "SELECT * from [UserInfoDatabase].[dbo].[UserExpected] where UserName=@UserName";
                }

                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = strSql;
                    if (!string.IsNullOrEmpty(flagDay))
                    {
                        cmd.Parameters.AddWithValue("@FlagDay", flagDay);
                    }
                    cmd.Parameters.AddWithValue("@UserName", userName);
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        result.UserExpecteds.Add(Convert.ToInt32(reader["UserExpected"]));
                    }
                }
            }
            catch { }
            return result;
        }

        //not useful
        private static void ExecuteUserOrderInfo(List<UserInfo> userInfos)
        {
            foreach (var v in userInfos)
            {
                v.UserOrderInfos = ExecuteUserOrderInfo(v.UserName);
            }
        }

        private static bool UpdateUserInfo(UserInfo userInfo)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = @"update [UserInfoDatabase].[dbo].[UserInfo] 
set UserPsw=@UserPsw, DisplayName=@DisplayName, UserRole=@UserRole, UserBalance=@UserBalance
 where UserName=@UserName";
                    cmd.Parameters.AddWithValue("@UserName", userInfo.UserName);
                    cmd.Parameters.AddWithValue("@DisplayName", userInfo.DisplayName);
                    cmd.Parameters.AddWithValue("@UserPsw", userInfo.UserPsw);
                    cmd.Parameters.AddWithValue("@UserRole", userInfo.UserRole);
                    cmd.Parameters.AddWithValue("@UserBalance", userInfo.UserBalance);
                    cmd.ExecuteNonQuery();
                }

            }
            catch
            {
                return false;
            }
            return true;
        }

        //更新Order表时注意使用oldorder,oldtime属性
        private static bool UpdateUserOrderInfo(string userName, List<UserOrderInfo> orderInfos)
        {
            if (orderInfos.Count == 1)
            {
                int oldCost = orderInfos[0].OldCost;
                int cost = orderInfos[0].UserCost;
                string OldOrder = orderInfos[0].OldOrder;
                string OldOrderTime = orderInfos[0].OldOrderTime;
                string UserOrder = orderInfos[0].UserOrder;
                string UserOrderTime = orderInfos[0].UserOrderTime;
                try
                {
                    using (SqlConnection conn = new SqlConnection(connString))
                    {
                        conn.Open();
                        SqlCommand cmd = conn.CreateCommand();
                        cmd.CommandText = @"update [UserInfoDatabase].[dbo].[UserOrderInfo] 
set UserOrder=@UserOrder, UserOrderTime=@UserOrderTime, UserCost=@UserCost
 where UserName=@UserName and UserOrder=@OldOrder and UserOrderTime=@OldOrderTime and UserCost=@OldCost";
                        cmd.Parameters.AddWithValue("@UserName", userName);
                        cmd.Parameters.AddWithValue("@OldOrder", OldOrder);
                        cmd.Parameters.AddWithValue("@OldOrderTime", OldOrderTime);
                        cmd.Parameters.AddWithValue("@UserOrder", UserOrder);
                        cmd.Parameters.AddWithValue("@UserOrderTime", UserOrderTime);
                        cmd.Parameters.AddWithValue("@OldCost", oldCost);
                        cmd.Parameters.AddWithValue("@UserCost", cost);
                        cmd.ExecuteNonQuery();
                    }

                }
                catch
                {
                    return false;
                }
                return true;
            }
            return false;
        }
        #endregion
    }

    public class UserInfo
    {
        public string UserName { get; set; }

        public string DisplayName { get; set; }

        public string UserPsw { get; set; }

        public List<UserOrderInfo> UserOrderInfos { get; set; }

        public int UserBalance { get; set; }

        public string UserRole { get; set; }
    }

    public class UserOrderInfo
    {
        public string UserOrder { get; set; }

        public int UserCost { get; set; }

        public string UserOrderTime { get; set; }

        public string FlagDay { get; set; }

        //只在update时使用
        public int OldCost { get; set; }

        public string OldOrder { get; set; }

        public string OldOrderTime { get; set; }
    }

    public class UserExpected
    {
        public string UserName { get; set; }
        public List<int> UserExpecteds { get; set; }
        public string FlagDay { get; set; }
    }

    public class MostExpectedInfo
    {
        public int Count { get; set; }

        public Orders Shop { get; set; }
    }

    //获取admin管理的信息，ps:订什么，能不能订餐
    public class AdminInfo
    {
        public List<string> Administrators { get; set; }

        public Orders WhatToEat { get; set; }

        public bool IfCanOrder { get; set; }
    }

    public enum Orders
    {
        Nothing,
        Kuaicanxiaozi,
        Meishimeike,
    }
}
